/**
 * Copyright 2018-2019 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.jianggujin.modulelink.util.JAssert;

/**
 * 默认Bean提供者服务实现
 * 
 * @author jianggujin
 *
 */
public class JDefaultBeanProvider extends JAbstractBeanProvider {

    private Map<String, Object> beans;

    public JDefaultBeanProvider() {
        this(new ConcurrentHashMap<String, Object>());
    }

    public JDefaultBeanProvider(Map<String, Object> beans) {
        JAssert.checkNotNull(beans, "beans must nut be null");
        this.beans = beans;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T optBean(Class<T> requiredType) {
        JAssert.checkNotNull(requiredType, "requiredType must nut be null");
        T obj = null;
        for (Object bean : this.beans.values()) {
            if (requiredType.isAssignableFrom(bean.getClass())) {
                JAssert.checkState(obj == null, "duplicated bean with requiredType:" + requiredType.getName());
                obj = (T) bean;
            }
        }
        return obj;
    }

    @Override
    public Object optBean(String name) {
        JAssert.checkNotNull(name, "name must nut be null");
        return this.beans.get(name);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T optBean(String name, Class<T> requiredType) {
        JAssert.checkNotNull(name, "name must nut be null");
        JAssert.checkNotNull(requiredType, "requiredType must nut be null");
        Object bean = beans.get(name);
        if (bean != null && requiredType.isAssignableFrom(bean.getClass())) {
            return (T) bean;
        }
        return null;
    }

    @Override
    public String[] optBeanNamesForType(Class<?> type) {
        JAssert.checkNotNull(type, "type must nut be null");
        List<String> names = new ArrayList<String>();
        for (Entry<String, Object> entry : this.beans.entrySet()) {
            if (type.isAssignableFrom(entry.getValue().getClass())) {
                names.add(entry.getKey());
            }
        }
        return names.isEmpty() ? null : names.toArray(new String[names.size()]);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> Map<String, T> optBeansOfType(Class<T> type) {
        JAssert.checkNotNull(type, "type must nut be null");
        Map<String, T> filterBeans = new HashMap<String, T>();
        for (Entry<String, Object> entry : this.beans.entrySet()) {
            if (type.isAssignableFrom(entry.getValue().getClass())) {
                filterBeans.put(entry.getKey(), (T) entry.getValue());
            }
        }
        return filterBeans.isEmpty() ? null : filterBeans;
    }

    public void putBean(String name, Object bean) {
        JAssert.checkNotNull(name, "name must nut be null");
        JAssert.checkNotNull(bean, "bean must nut be null");
        this.beans.put(name, bean);
    }
}
